/*
 * @(#)PageData.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.util.common;

import java.util.ArrayList;
import java.util.List;

/**
 * PageData
 * 
 * @author BIS 2012-12-24
 */
public class PageData
{

    /**
     * DEFAULT
     */
    public static final PageData DEFAULT       = new PageData();

    /**
     * pageSize
     */
    protected int                pageSize      = 10;

    /**
     * rowCount
     */
    protected int                rowCount      = 0;

    /**
     * pageCount
     */
    protected int                pageCount     = 0;

    /**
     * currPage
     */
    protected int                currPage      = 1;

    /**
     * startPosition
     */
    protected int                startPosition = 0;

    /**
     * endPosition
     */
    protected int                endPosition   = 0;

    /**
     * list
     */
    protected List               list;

    /**
     * appendix
     */
    protected Object             appendix;

    /**
     * currGroup
     */
    protected int                currGroup     = 1;

    /**
     * oldGroup
     */
    protected int                oldGroup      = 1;
    /**
     * groupPage
     */
    protected int                groupPage     = 0;

    /**
     * getCurrGroup
     * @Description:
     * @return
     */
    public int getCurrGroup()
    {
        return currGroup;
    }

    public int getOldGroup()
    {
        return oldGroup;
    }

    /**
     * PageData
     * @param currPage
     * @param pageSize
     * @param rowCount
     * @param list
     */
    public PageData(int currPage, int pageSize, int rowCount, List list)
    {
        this.currPage = currPage;
        this.pageSize = pageSize;
        this.rowCount = rowCount;
        this.list = list;
        init();
    }

    /**
     * PageData
     * @param currPage
     * @param pageSize
     * @param rowCount
     */
    public PageData(int currPage, int pageSize, int rowCount)
    {
        this.currPage = currPage;
        this.pageSize = pageSize;
        this.rowCount = rowCount;
        this.list = list;
        init();
    }

    /**
     * PageData
     * @param page
     * @param list
     */
    public PageData(Page page, List list)
    {
        this.currPage = page.getPage();
        this.pageSize = page.getPageSize();
        this.rowCount = page.getRowCount();
        this.list = list;
        init();
    }

    /**
     * PageData
     * @param currPage
     * @param pageSize
     * @param list
     */
    public PageData(int currPage, int pageSize, List list)
    {
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.list = setData(list);
    }

    /**
     * PageData
     * @param currPage
     * @param pageSize
     * @param tempList
     * @param groupPage
     * @param oldGroup
     * @param currGroup
     */
    public PageData(int currPage, int pageSize, List tempList, int groupPage, int oldGroup, int currGroup)
    {
        this.currPage = currPage;
        this.pageSize = pageSize;
        this.oldGroup = oldGroup;
        this.currGroup = currGroup;
        this.rowCount = tempList.size();
        this.groupPage = groupPage;
        int startLine = 0;
        if (groupPage > 0)
            startLine = (currPage - 1) % groupPage * pageSize;
        int endLine = startLine + pageSize;
        int len = tempList.size();
        if (endLine > len)
            endLine = len;
        List dataList = new ArrayList();
        for (int i = startLine; i < endLine; i++)
        {
            dataList.add(tempList.get(i));
        }

        if (startLine >= len && len > 10)
        {
            for (int i = len - 10; i < len; i++)
            {
                dataList.add(tempList.get(i));
            }
            this.currPage -= 1;
        }
        this.list = dataList;
        if (currPage <= 0)
            currPage = 1;
        int start = (currPage - 1) * pageSize; // ��λ
        int end = start + pageSize;
        if (end > rowCount + (currGroup - 1) * pageSize * groupPage)
        {
            end = rowCount + (currGroup - 1) * pageSize * groupPage;
        }
        if (rowCount != 0)
        {
            startPosition = start + 1;
            endPosition = end;
        }
    }

    /**
     * setData
     * @Description:
     * @param list
     * @return
     */
    private List setData(List list)
    {
        rowCount = list.size();
        pageCount = rowCount % pageSize == 0 ? rowCount / pageSize : rowCount / pageSize + 1;
        if (currPage <= 0)
            currPage = 1;
        if (pageCount > 0 && currPage > pageCount)
            currPage = pageCount;
        int absolute = (currPage - 1) * pageSize; // ��λ
        int end = absolute + pageSize;
        if (end > rowCount)
        {
            end = rowCount;
        }
        startPosition = absolute + 1;
        endPosition = end;
        return list.subList(absolute, end);
    }

    /**
     * PageData
     */
    public PageData()
    {
        list = new ArrayList();
    }

    /**
     * toString
     */
    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("pageSize = ").append(pageSize).append(" ; \r\n");
        sb.append("rowCount = ").append(rowCount).append(" ; \r\n");
        sb.append("pageCount = ").append(pageCount).append(" ; \r\n");
        sb.append("currPage = ").append(currPage).append(" ; \r\n");
        sb.append("startPosition = ").append(startPosition).append(" ; \r\n");
        sb.append("endPosition = ").append(endPosition).append(" ; \r\n");
        sb.append("list = \r\n").append(list).append(" ; \r\n");
        return sb.toString();
    }

    /**
     * init
     * @Description:
     */
    public void init()
    {
        if (rowCount != 0)
            pageCount = rowCount % pageSize == 0 ? rowCount / pageSize : rowCount / pageSize + 1;
        if (currPage <= 0)
            currPage = 1;
        if (currPage > pageCount)
            currPage = pageCount;
        int start = (currPage - 1) * pageSize; // ��λ
        int end = start + pageSize;
        if (end > rowCount)
        {
            end = rowCount;
        }
        if (rowCount != 0)
        {
            startPosition = start + 1;
            endPosition = end;
        }
    }

    /**
     * @return the pageSize
     */
    public int getPageSize()
    {
        return pageSize;
    }

    /**
     * @param pageSize the pageSize to set
     */
    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }

    /**
     * @return the rowCount
     */
    public int getRowCount()
    {
        return rowCount;
    }

    /**
     * @param rowCount the rowCount to set
     */
    public void setRowCount(int rowCount)
    {
        this.rowCount = rowCount;
    }

    /**
     * @return the pageCount
     */
    public int getPageCount()
    {
        return pageCount;
    }

    /**
     * @param pageCount the pageCount to set
     */
    public void setPageCount(int pageCount)
    {
        this.pageCount = pageCount;
    }

    /**
     * @return the currPage
     */
    public int getCurrPage()
    {
        return currPage;
    }

    /**
     * @param currPage the currPage to set
     */
    public void setCurrPage(int currPage)
    {
        this.currPage = currPage;
    }

    /**
     * @return the startPosition
     */
    public int getStartPosition()
    {
        return startPosition;
    }

    /**
     * @param startPosition the startPosition to set
     */
    public void setStartPosition(int startPosition)
    {
        this.startPosition = startPosition;
    }

    /**
     * @return the endPosition
     */
    public int getEndPosition()
    {
        return endPosition;
    }

    /**
     * @param endPosition the endPosition to set
     */
    public void setEndPosition(int endPosition)
    {
        this.endPosition = endPosition;
    }

    /**
     * @return the list
     */
    public List getList()
    {
        return list;
    }

    /**
     * @param list the list to set
     */
    public void setList(List list)
    {
        this.list = list;
    }

    /**
     * @return the appendix
     */
    public Object getAppendix()
    {
        return appendix;
    }

    /**
     * @param appendix the appendix to set
     */
    public void setAppendix(Object appendix)
    {
        this.appendix = appendix;
    }

    /**
     * @return the groupPage
     */
    public int getGroupPage()
    {
        return groupPage;
    }

    /**
     * @param groupPage the groupPage to set
     */
    public void setGroupPage(int groupPage)
    {
        this.groupPage = groupPage;
    }

    /**
     * @param currGroup the currGroup to set
     */
    public void setCurrGroup(int currGroup)
    {
        this.currGroup = currGroup;
    }

    /**
     * @param oldGroup the oldGroup to set
     */
    public void setOldGroup(int oldGroup)
    {
        this.oldGroup = oldGroup;
    }

}
